package com.yhh.zhongdian.view.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.yhh.basemvplib.impl.IPresenter;
import com.yhh.zhongdian.R;
import com.yhh.zhongdian.base.MBaseActivity;
import com.yhh.zhongdian.constant.RxBusTag;
import com.yhh.zhongdian.help.ReadBookControl;
import com.yhh.zhongdian.utils.theme.ATH;
import com.yhh.zhongdian.utils.theme.ThemeStore;
import com.yhh.zhongdian.widget.page.animation.PageAnimation;
import com.yhh.zhongdian.widget.views.ATESwitch;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by GKF on 2017/12/16.
 * 书源管理
 */
public class BookSettingsActivity extends MBaseActivity {
    private ReadBookControl readBookControl;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_content)
    LinearLayout llContent;

    @BindView(R.id.vw_bg)
    View vw_bg;
    @BindView(R.id.sbImmersionStatusBar)
    ATESwitch sbImmersionStatusBar;

    @BindView(R.id.sw_volume_next_page)
    ATESwitch sw_volume_next_page;

    @BindView(R.id.sb_hideStatusBar)
    ATESwitch sb_hideStatusBar;

    @BindView(R.id.sb_hideNavigationBar)
    ATESwitch sb_hideNavigationBar;

    @BindView(R.id.sb_click)
    ATESwitch sb_click;

    @BindView(R.id.sb_show_title)
    ATESwitch sb_show_title;

    @BindView(R.id.sb_showLine)
    ATESwitch sb_showLine;

    @BindView(R.id.sb_select_text)
    ATESwitch sb_select_text;

    @BindView(R.id.sb_showTimeBattery)
    ATESwitch sb_showTimeBattery;
    @BindView(R.id.sb_to_lh)
    ATESwitch sb_to_lh;

    @BindView(R.id.sw_read_aloud_key)
    ATESwitch sw_read_aloud_key;
    @BindView(R.id.sb_click_all_next)
    ATESwitch sb_click_all_next;

    @BindView(R.id.llNavigationBarColor)
    LinearLayout llNavigationBarColor;

    @BindView(R.id.llScreenTimeOut)
    LinearLayout llScreenTimeOut;

    @BindView(R.id.llJFConvert)
    LinearLayout llJFConvert;

    @BindView(R.id.ll_screen_direction)
    LinearLayout ll_screen_direction;
    @BindView(R.id.ll_page_mode)
    LinearLayout ll_page_mode;
    @BindView(R.id.ll_text_index)
    LinearLayout ll_text_index;
    @BindView(R.id.tv_text_index)
    TextView tv_text_index;

    @BindView(R.id.tv_screen_time_out)
    TextView tv_screen_time_out;

    @BindView(R.id.tvJFConvert)
    TextView tvJFConvert;

    @BindView(R.id.tv_screen_direction)
    TextView tv_screen_direction;
    @BindView(R.id.tv_page_mode_select)
    TextView tv_page_mode_select;
    @BindView(R.id.reNavBarColor_val)
    TextView reNavBarColor_val;


    public static void startThis(Context context) {
        context.startActivity(new Intent(context, BookSettingsActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected IPresenter initInjector() {
        return null;
    }

    @Override
    protected void onCreateActivity() {
        readBookControl = ReadBookControl.getInstance();
        getWindow().getDecorView().setBackgroundColor(ThemeStore.backgroundColor(this));
        setContentView(R.layout.activity_book_settings);
        ButterKnife.bind(this);
        RxBus.get().register(this);
        this.setSupportActionBar(toolbar);
        setupActionBar();
        vw_bg.setOnClickListener(null);
    }


    //设置ToolBar
    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("阅读设置");
        }
    }

    @Override
    public void finish() {
        RxBus.get().unregister(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    //菜单
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 数据初始化
     */
    @Override
    protected void initData() {
        // 屏幕方向
        upScreenDirection(readBookControl.getScreenDirection());
        // 翻页模式
        upPageMode(readBookControl.getPageMode());
        upScreenTimeOut(readBookControl.getScreenTimeOut());
        upFConvert(readBookControl.getTextConvert());
        upNavBarColor(readBookControl.getNavBarColor());
        upTextIndent(readBookControl.getIndent());

        sbImmersionStatusBar.setChecked(readBookControl.getImmersionStatusBar());
        sw_volume_next_page.setChecked(readBookControl.getCanKeyTurn());
        sw_read_aloud_key.setChecked(readBookControl.getAloudCanKeyTurn());
        sb_hideStatusBar.setChecked(readBookControl.getHideStatusBar());
        sb_to_lh.setChecked(readBookControl.getToLh());
        sb_hideNavigationBar.setChecked(readBookControl.getHideNavigationBar());
        sb_click.setChecked(readBookControl.getCanClickTurn());
        sb_click_all_next.setChecked(readBookControl.getClickAllNext());
        sb_show_title.setChecked(readBookControl.getShowTitle());
        sb_showTimeBattery.setChecked(readBookControl.getShowTimeBattery());
        sb_showLine.setChecked(readBookControl.getShowLine());
        sb_select_text.setChecked(readBookControl.isCanSelectText());
        upView();
    }


    void upView() {
        if (readBookControl.getHideStatusBar()) {
            sb_showTimeBattery.setEnabled(true);
            sb_to_lh.setEnabled(true);
        } else {
            sb_showTimeBattery.setEnabled(false);
            sb_to_lh.setEnabled(false);
        }

        sw_read_aloud_key.setEnabled(readBookControl.getCanKeyTurn());
        sb_click_all_next.setEnabled(readBookControl.getCanClickTurn());
        if (readBookControl.getHideNavigationBar()) {
            llNavigationBarColor.setEnabled(false);
            reNavBarColor_val.setEnabled(false);
        } else {
            llNavigationBarColor.setEnabled(true);
            reNavBarColor_val.setEnabled(true);
        }
    }


    void upScreenTimeOut(int screenTimeOut) {
        tv_screen_time_out.setText(getContext().getResources().getStringArray(R.array.screen_time_out)[screenTimeOut]);
    }


    void upFConvert(int fConvert) {
        tvJFConvert.setText(getContext().getResources().getStringArray(R.array.convert_s)[fConvert]);
    }


    void upScreenDirection(int screenDirection) {
        String[] screenDirectionListTitle = getContext().getResources().getStringArray(R.array.screen_direction_list_title);
        if (screenDirection >= screenDirectionListTitle.length) {
            tv_screen_direction.setText(screenDirectionListTitle[0]);
        } else {
            tv_screen_direction.setText(screenDirectionListTitle[screenDirection]);
        }
    }


    void upPageMode(int pageMode) {
        tv_page_mode_select.setText(String.format("%s", PageAnimation.Mode.getPageMode(pageMode)));
    }


    void upNavBarColor(int nColor) {
        reNavBarColor_val.setText(getContext().getResources().getStringArray(R.array.NavBarColors)[nColor]);
    }

    void upTextIndent(int indent) {
        tv_text_index.setText(getContext().getResources().getStringArray(R.array.indent)[indent]);
    }


    @Override
    protected void bindEvent() {
        super.bindEvent();

        sbImmersionStatusBar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setImmersionStatusBar(isChecked);
                    onUpBar();
                    RxBus.get().post(RxBusTag.RECREATE, true);
                }
            }
        });

        sb_hideStatusBar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setHideStatusBar(isChecked);
                    onRecreate();
                }
            }
        });

        sb_to_lh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setToLh(isChecked);
                    onRecreate();
                }
            }
        });

        sb_hideNavigationBar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setHideNavigationBar(isChecked);
                    initData();
                    onRecreate();
                }
            }
        });

        sw_volume_next_page.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setCanKeyTurn(isChecked);
                    upView();
                }
            }
        });

        sw_read_aloud_key.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setAloudCanKeyTurn(isChecked);
                }
            }
        });

        sb_click.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setCanClickTurn(isChecked);
                    upView();
                }
            }
        });

        sb_click_all_next.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setClickAllNext(isChecked);
                }
            }
        });

        sb_show_title.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setShowTitle(isChecked);
                    onRefreshPage();
                }
            }
        });

        sb_showTimeBattery.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setShowTimeBattery(isChecked);
                    onRefreshPage();
                }
            }
        });

        sb_showLine.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setShowLine(isChecked);
                    onRefreshPage();
                }
            }
        });

        llScreenTimeOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle(getContext().getString(R.string.keep_light))

                        .setSingleChoiceItems(
                                getContext().getResources().getStringArray(R.array.screen_time_out),
                                readBookControl.getScreenTimeOut(),
                                (DialogInterface dialogInterface, int i) -> {
                                    readBookControl.setScreenTimeOut(i);
                                    upScreenTimeOut(i);
                                    onKeepScreenOnChange(i);
                                    dialogInterface.dismiss();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });


        llJFConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle(getContext().getString(R.string.jf_convert))
                        .setSingleChoiceItems(
                                getContext().getResources().getStringArray(R.array.convert_s),
                                readBookControl.getTextConvert(),
                                (DialogInterface dialogInterface, int i) -> {
                                    readBookControl.setTextConvert(i);
                                    upFConvert(i);
                                    dialogInterface.dismiss();
                                    onRefreshPage();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });

        ll_screen_direction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle(getContext().getString(R.string.screen_direction))
                        .setSingleChoiceItems(
                                getContext().getResources().getStringArray(R.array.screen_direction_list_title),
                                readBookControl.getScreenDirection(),
                                (DialogInterface dialogInterface, int i) -> {
                                    readBookControl.setScreenDirection(i);
                                    upScreenDirection(i);
                                    dialogInterface.dismiss();
                                    onRecreate();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });


        ll_page_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle(getContext().getString(R.string.page_mode))
                        .setSingleChoiceItems(
                                PageAnimation.Mode.getAllPageMode(),
                                readBookControl.getPageMode(),
                                (DialogInterface dialogInterface, int i) -> {
                                    readBookControl.setPageMode(i);
                                    upPageMode(i);
                                    dialogInterface.dismiss();
                                    onUpPageMode();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });

        ll_text_index.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext(), R.style.alertDialogTheme)
                        .setTitle(getContext().getString(R.string.indent))
                        .setSingleChoiceItems(getContext().getResources().getStringArray(R.array.indent),
                                readBookControl.getIndent(),
                                (dialogInterface, i) -> {
                                    readBookControl.setIndent(i);
                                    upTextIndent(i);
                                    dialogInterface.dismiss();
                                    onRefreshPage();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });

        llNavigationBarColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle(getContext().getString(R.string.re_navigation_bar_color))
                        .setSingleChoiceItems(
                                getContext().getResources().getStringArray(R.array.NavBarColors),
                                readBookControl.getNavBarColor(),
                                (DialogInterface dialogInterface, int i) -> {
                                    readBookControl.setNavBarColor(i);
                                    upNavBarColor(i);
                                    dialogInterface.dismiss();
                                    onRecreate();
                                })
                        .create();
                dialog.show();
                ATH.setAlertDialogTint(dialog);
            }
        });

        sb_select_text.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    readBookControl.setCanSelectText(isChecked);
                }
            }
        });
    }

    public void onUpBar() {
        RxBus.get().post(RxBusTag.BOOK_SETTING_UP_BAR, true);
    }

    public void onKeepScreenOnChange(int keepScreenOn) {
        RxBus.get().post(RxBusTag.BOOK_SETTING_KEEP_SCREEN_ONCHANGE, keepScreenOn);
    }

    public void onRecreate() {
        RxBus.get().post(RxBusTag.BOOK_SETTING_RECREATE, true);
    }

    public void onRefreshPage() {
        RxBus.get().post(RxBusTag.BOOK_SETTING_REFRESH, true);
    }

    public void onUpPageMode() {
        RxBus.get().post(RxBusTag.BOOK_SETTING_UP_PAGE_MODE, true);
    }
}
